// pages/personal/medicalfeed.js
const app = getApp()
Page({
    /**
     * 页面的初始数据
     */
    data: {
        page: 1,
        respData: [],
        showLoadMoreBtn: true,
        userPhone: '',
        tagTitle: ['待反馈', '继续努力', '效果不错']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onShow(options) {
        this.loadData(this.data.page);
    },
    // 加载数据
    loadData: function (page) {
        let that = this;
        let cacheUserInfo = wx.getStorageSync('user_phone');
        let user_phone = (cacheUserInfo.phoneNumber && typeof (cacheUserInfo.phoneNumber) != 'undefined') ? cacheUserInfo.phoneNumber : '15378438289';
        this.setData({
            userPhone: user_phone
        });
        wx.request({
            url: app.globalData.domain + 'v4/index/getUserMedicalQuestion',
            method: 'POST',
            data: {
                user_phone: user_phone,
                page: page
            },
            success: function (res) {
                console.log(res)
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg
                    })
                    return;
                } else {
                    // 请求成功, 页面展示 体质查询结果
                    for (var i = 0; i < res.data.data.data.length; i++) {
                        that.data.respData.push(res.data.data.data[i]);
                    }

                    that.setData({
                        respData: that.data.respData,
                        page: page,
                        showLoadMoreBtn: (res.data.data.last_page == page) ? false : true
                    });
                    console.log('respData::', that.data.respData);
                }
                return;
            }
        })
    },
    // 加载更多
    loadmore: function () {
        let curpage = this.data.page + 1;
        console.log('加载页码::', curpage);
        this.loadData(curpage);
    },
    // 反馈
    feedback: function (e) {
        let feedType = e.target.dataset.type;
        let feedIds = e.target.dataset.id;
        let that = this;
        wx.request({
            url: app.globalData.domain + 'v4/index/dealMedicalFeedback',
            method: 'POST',
            data: {
                feedType: feedType,
                userPhone: that.data.userPhone,
                feedIds: feedIds
            },
            success: function (res) {
                if (res.data.code != 0) {
                    wx.showModal({
                        title: '温馨提示',
                        showCancel: false,
                        content: res.data.msg,
                        success: function() {
                            wx.redirectTo({
                                url: '/pages/personal/medicalfeed'
                            });
                        }                        
                    })

                    return;
                } else {
                    wx.showModal({
                        title: '温馨提示',
                        content: '感谢您的反馈!',
                        success: function() {
                            wx.redirectTo({
                                url: '/pages/personal/medicalfeed'
                            });                            
                        }
                    });
                }
                return;
            }
        })
    }
})